<template>
  <!-- 评论 -->
  <div class="commentItem">
    <!-- 头像 & 点赞 -->
    <div class="info-box">
      <img :src="getimgurl" @click="addping" />
      <div class="name-box">
        <span class="name">{{ item.author && item.author.nickname }}</span>
        <span class="time">{{ item.created_at | formatTime }}</span>
      </div>
      <div class="zan" @click="star">
        <span>{{ item.star }}</span>
        <i class="iconfont iconbtn_dianzan_small_nor"></i>
      </div>
    </div>
    <!-- 评论内容 -->
    <div class="comment_content">
      <div class="content">{{ item.content }}</div>
      <!-- 回复 -->
      <div class="comment_answer">
        <div
          class="answer"
          v-for="(chileditem, childindex) in item.children_comments"
          :key="childindex"
        >
          <span class="name">{{ chileditem.author }}:</span>
          <span class="reply_content">{{ chileditem.content }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { starArticleComment } from '@/api/api.js'
export default {
  props: ['item'],
  data () {
    return {
      date: process.env.VUE_APP_URL
    }
  },
  computed: {
    getimgurl () {
      if (this.item.author && this.item.author.avatar) {
        if (this.item.author.avatar.includes(this.date)) {
          return this.item.author.avatar
        } else {
          return this.date + this.item.author.avatar
        }
      } else {
        return ''
      }
    }
  },

  methods: {
    async star () {
      await starArticleComment()
    },
    // 添加评论
    addping () {
      this.pinglun = 'comment'
      this.$emit('showCommentitem', this.item.id)
    }
  }
}
</script>

<style lang="less" scoped>
.commentItem {
  .info-box {
    margin-top: 15px;
    display: flex;
    align-items: center;
    img {
      width: 35px;
      height: 35px;
      border-radius: 50%;
      margin-right: 9px;
    }
    .name-box {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      .name {
        font-size: 12px;
        font-weight: 600;
        color: #545671;
      }
      .time {
        padding-top: 5px;
        font-size: 12px;
        color: #b4b4bd;
      }
    }
    .zan {
      span {
        color: #b4b4bd;
        font-size: 12px;
      }
      .iconfont {
        color: #b4b4bd;
        font-size: 24px;
      }
    }
  }
  .comment_content {
    padding-left: 44px;
    .content {
      margin-top: 11px;
      font-size: 16px;
      color: #181a39;
      line-height: 27px;
    }
    .comment_answer {
      margin-top: 5px;
      background: #f7f4f5;
      .answer {
        font-size: 14px;
        color: #545671;
        padding: 10px;
        .name {
          font-weight: 500;
          margin-right: 5px;
          color: #000;
        }
        .reply_content {
          color: #b4b4bd;
        }
      }
    }
  }
}
</style>
